package com.zuo.mall.controller;

import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.common.api.CommonResult;
import com.zuo.mall.dto.PmsProductAttributeParam;
import com.zuo.mall.dto.ProductAttrInfo;
import com.zuo.mall.entity.PmsProductAttribute;
import com.zuo.mall.service.PmsProductAttributeService;
import com.zuo.mall.service.PmsProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("商品属性管理")
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {







    @Autowired
    private PmsProductAttributeService productAttributeService;
    @ApiOperation("通过属性id获取属性,修改前回显数据")
    @GetMapping("{attributeId}")
    public CommonResult getItem(@PathVariable("attributeId") Long attributeId ) {
        PmsProductAttribute productAttribute = productAttributeService.getItem(attributeId);
        return CommonResult.success(productAttribute);
    }

    @ApiOperation("修改数据")
    @PostMapping("/update/{attributeId}")
    public CommonResult update(@PathVariable("attributeId") Long attributeId,
                               @RequestBody PmsProductAttributeParam productAttributeParam) {

        int count  = productAttributeService.update(attributeId, productAttributeParam);

        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据商品分类id获取商品属性及其分类")
    @GetMapping("attrInfo/{productCategoryId}")
    public CommonResult<List<ProductAttrInfo>> getAttrInfo(@PathVariable("productCategoryId") Long productCategoryId) {
        List<ProductAttrInfo> result =  productAttributeService.getProductAttrInfo(productCategoryId);
        return CommonResult.success(result);
    }


    @ApiOperation("根据分类查询属性列表或参数列表")
    @GetMapping("/list/{attributeCategoryId}")
    public CommonResult<CommonPage<PmsProductAttribute>> getList(
            @PathVariable("attributeCategoryId") Long attributeCategoryId,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "type", required = false) Integer type
            ) {
        CommonPage<PmsProductAttribute> commonPage = productAttributeService.listByAttributeCategoryId(attributeCategoryId, pageNum, pageSize, type);
        return CommonResult.success(commonPage);
    }


    @PostMapping("create")
    @ApiOperation("添加商品参数")
    public CommonResult create(@RequestBody PmsProductAttributeParam pmsProductAttributeParam) {
        int count = productAttributeService.create(pmsProductAttributeParam);
        if (count >0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("批量删除商品属性")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = productAttributeService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
